/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.itip.tivoli;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-9 下午2:24:35          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ConfManager {

    private static final Logger logger = Logger.getLogger(ConfManager.class);

    private static ConfManager instance = null;

    private static Object _lock = new Object();

    private List<ConfInfo> confList = new ArrayList<ConfInfo>();

    private Map<String, ConfInfo> confMap = new HashMap<String, ConfInfo>();

    public Map<String, String> sqlMap = new HashMap<String, String>();

    private ConfManager() {
        initConf();
    }

    public static synchronized ConfManager getInstance() {
        if (instance == null) {
            synchronized (_lock) {
                instance = new ConfManager();
            }
        }
        return instance;
    }

    @SuppressWarnings("unchecked")
    private void initConf() {
        SAXReader reader = new SAXReader();
        try {
            Document itip = reader.read("itip-conf/tivoli.xml");
            //历史性能
            List<Element> elements = itip.selectNodes("/tivoli/perf_conf");
            for (Element e : elements) {
                ConfInfo conf = new ConfInfo();
                conf.setResType(e.elementText("resType").trim());
                conf.setSql(e.elementText("sql").trim());
                conf.setProps(e.elementText("props"));
                confList.add(conf);
                confMap.put(conf.getResType(), conf);
            }

            //主资源发现
            List<Element> resElements = itip.selectNodes("/tivoli/res_conf");
            for (Element e : resElements) {
                String type = e.elementText("type").trim();
                String sql = e.elementText("sql").trim();
                sqlMap.put(type, sql);

            }
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public List<ConfInfo> getConfList() {
        return confList;
    }

    public Map<String, ConfInfo> getConfMap() {
        return confMap;
    }

    public static void main(String[] args) {

        if (System.getProperty("javax.xml.parsers.SAXParserFactory") == null) {
            System.setProperty("javax.xml.parsers.SAXParserFactory", "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
        }
        ConfManager.getInstance();
    }

}
